
package xml;

import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.File;
import java.util.List;
import java.util.Iterator;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;




/**
 * Tests DocumentReader
 * 
 * @author  joe.bowbeer
 */
public class ReaderTest {

    /** Creates new ReaderTest */
    public ReaderTest() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) throws Exception {

        /* XMLWriter for viewing SAX events. */

        XMLWriter echo = new XMLWriter();

        /* Build document from xml file. */

        SAXBuilder builder = new SAXBuilder();
        builder.setXMLFilter(echo);
        InputStream in = ReaderTest.class.getResourceAsStream("F4311Z1.xml");


        Document doc = builder.build(in);



        XMLReader parser = new DocumentReader(doc);
        echo.setParent(parser);
        StringWriter writer = new StringWriter();
        parser = new XMLWriter(echo, writer);
        parser.parse((InputSource)null);



        builder = new SAXBuilder();
        builder.setXMLFilter(echo);
        String xml = writer.toString();


        doc = builder.build(new StringReader(xml));


        XMLOutputter outputter = new XMLOutputter();
        outputter.output(doc, System.out);


       //Document doc = builder.build(new File(filename));
      //  String dni = clients.getChild("person").getAttributeValue("dni");


       Element clients = doc.getRootElement();
       List persons = clients.getChildren();
       Iterator it = persons.iterator();

        while (it.hasNext())
        {
         Element person = (Element)it.next();
         System.out.println(person.getChild("company").getTextTrim());
         System.out.println(person.getChild("OrderNumber").getTextTrim());
         }




    }

}
